package com.example.dllo.my36kr.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.VideoBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 氪TV适配器
 * Created by CPC_272 on 16/8/27.
 */
public class VideoAdapter extends BaseAdapter {
     private List<VideoBean.DataBean.DataBean1>dataBean1s;
     private Context context;

    //记录下来哪个视频是播放的
    private int playPos = -1;//默认是没有播放的
    private VideoHodler videoHodler = null;//记录播放的VH

    /**
     *  当点击行布局时  调用方法 会传回一个videoHodler
     *  而方法内做出反应 停止正在播放的videoView同时显示图片
     */

    public VideoAdapter setVideoHodler(VideoHodler videoHodler) {
        //判断当传回videoHodler!= null时才暂停
        //而第一次点击是没有videoView正在播放
        //videoHodler总是记录上一次播放的那条
        //而判断执行之后
        if (this.videoHodler != null) {
            this.videoHodler.videoView.pause();
            this.videoHodler.image_video.setVisibility(View.VISIBLE);
        }
        //在这里才将当前点击的videoHodler赋值过来
        this.videoHodler = videoHodler;
        return this;
    }

    public VideoAdapter(Context context) {
        this.context = context;
    }


    public void setDataBean1s(List<VideoBean.DataBean.DataBean1> dataBean1s) {
        this.dataBean1s = dataBean1s;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataBean1s!=null?dataBean1s.size():0;

    }

    @Override
    public Object getItem(int position) {
        return dataBean1s!=null ?dataBean1s.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 外部传进来的position赋值给playPos
     * @param playPos
     */
    public void setPlayPos(int playPos) {
        this.playPos = playPos;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         VideoHodler holder=null;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_video,parent,false);
            holder=new VideoHodler(convertView);
            convertView.setTag(holder);
        }else{
            holder= (VideoHodler) convertView.getTag();
        }

        /**
         * 判断当playPos不等于position时
         * 就是未点击 此时videoView是停止的 图片是显示的
         * 而 当点击后 Activity传进来的position赋值给playPos
         * 此时playPos == position
         * 所以走else方法 图片消失而videoView开始播放
         *
         */

        if (playPos != position) {
            holder.videoView.pause();
            holder.image_video.setVisibility(View.VISIBLE);
        } else {
            holder.image_video.setVisibility(View.GONE);
            holder.videoView.start();
        }



        holder.tv_title.setText(dataBean1s.get(position).getTv().getTitle());
        holder.tv_time.setText(dataBean1s.get(position).getTv().getDuration());
        Picasso.with(context).load(dataBean1s.get(position).getTv().getFeatureImg()).into(holder.image_video);
        return convertView;
    }
    public class VideoHodler {
        private TextView tv_title, tv_time;
        private ImageView image_video;
        private VideoView videoView;
        public VideoHodler(View view) {
            videoView = (VideoView) view.findViewById(R.id.videoview);
            image_video = (ImageView) view.findViewById(R.id.img_video);
            tv_title = (TextView) view.findViewById(R.id.title_video);
            tv_time = (TextView) view.findViewById(R.id.time_video);
        }
    }
}
